Back to course home
0% completed
Vote For New Content
Trapping Rain Water (hard)
Problem Statement
Given an array of positive integers height, where height[i] represents the height of the bar in the elevation map and each bar has width 1, return how much water it can trap after raining.
Examples
Example 1:
- Input: height =
[4, 0, 3, 0, 2]
- Expected Output:
5 - Justification: The first and third bars form a container that traps 3 units of water. The third and fifth bars trap an additional 2 units. Therefore, the total trapped water is 5 units.
Example 2:
- Input: height =
[1, 2, 1, 2, 1]
- Expected Output:
1 - Justification: Water is only trapped between the second and fourth bars, holding 1 unit of water.
Example 3:
- Input: height =
[3, 1, 2, 4, 0, 1, 3]
- Expected Output:
8 - Justification: The first, and fourth bars trap 3 units of water. The fourth, and seventh bars trap an additional 5 units. The total is 8 units of trapped water.
Constraints:
- n == height.length
- 1 <= n <= 2 * 104
- 0 <= height[i] <= 105
Try it yourself
Try solving this question here:
Java
Saved to Cloud
. . . .
Solution: Maximum Score of a Good Subarray
Solution: Trapping Rain Water
Mark as Completed
On this page
Problem Statement
Examples
Try it yourself